<?php
class ItemNewForm extends Form
{
	private $cachefile='ItemNew';
	
	function __construct()
	{				
		Form::Form('ItemNewForm');
	}
	
	function on_submit()
	{
		if(User::is_admin())
		{
			$submit 		= Url::get('submit');
			
			if($submit == "Cập nhật" || $submit == "Xóa tất cả")
			{
				$title_new 	= Url::get('title_new');
				$item_ids 		= Url::get('item_ids');
					
				$config_update = array(
											'title' 	=> $title_new,
											'item_ids'	=> implode(',',explode(',',$item_ids)),
											);
											
				###########################
				if(isset(CGlobal::$configs['ItemNew']))
					DB::update('configs',array("conf_val"=>addslashes(serialize($config_update))),"conf_key='ItemNew'", __FILE__." Dòng:".__LINE__);
				else
					DB::insert('configs',array("conf_key"=>'ItemNew',"conf_val"=>addslashes(serialize($config_update))), __FILE__." Dòng:".__LINE__);
				
				AZLib::get_config(0,1);
			}
			
			if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']){
				$referer = $_SERVER['HTTP_REFERER'];
			}
			else{
				$referer = $_SERVER['REQUEST_URI'];
			}
			
			Url::redirect_url($referer);
		}
		
		Url::redirect_url($_SERVER['REQUEST_URI']);
	}
	
	function draw()
	{
		global $display;
		
		$item_ids 	= '';
		$item_news 	= array();
		
		$configs = @CGlobal::$configs['ItemNew']['conf_val'];
		
		if($configs)
		{
			$configs = unserialize(stripslashes($configs));
			
			$title_new 	= $configs['title'];
			$item_ids 	= $configs['item_ids'];
		}
		else
		{
			$title_new 	= "SẢN PHẨM MỚI";
			$item_ids 	= '';
		}
		
		
		if($item_ids)
		{
			$re  = DB::query("SELECT * FROM item WHERE id IN($item_ids) AND status = 1 ORDER BY find_in_set(id,'$item_ids')");
			
			if($re)
			{
				while($product = mysql_fetch_assoc($re))
				{
					if($product['img_url'])
					{
						$product['image'] = AZLib::getImageThumb($product['img_url'],160,150,0,$product['img_server']);
					}
					else
					{    
						$product['image'] = "";//"style/images/no-images-35x35.gif";
					}
					
					if(isset(CGlobal::$allCategories[$product['category_id']])){
						$product['url'] = WEB_DIR.CGlobal::$allCategories[$product['category_id']]['nice_name']."/p{$product['id']}/".AZLib::safe_title($product['name']).".html";
					}
					else{	
						$product['url'] = WEB_DIR."p{$product['id']}/".AZLib::safe_title($product['name']).".html";
					}

					$product['name']  	= stripslashes($product['name']);
					
					
					if($product['list_brief'] == '')
					{
						$product['list_brief'] = $product['brief'];
					}
					
					$product['list_brief'] 	= AZLib::remove_4_js(AZLib::plainText(AZLib::parseBBCode(html_entity_decode($product['list_brief'], ENT_QUOTES, "UTF-8"))));
					$product['list_brief'] 	= preg_replace("/\\[([\s]*[0-9]{1,2}[\s]*)\]/eis", " ",$product['list_brief']);
					$product['list_brief']  	= str_replace(array("'","\""), array(""," "),$product['list_brief']);				
					$product['list_brief'] 	= AZLib::word_limit($product['list_brief'],50,'');
					
					$product['list_brief'] = '<b>'.str_replace(array("'","\""), array(""," "),$product['name']) . '</b><br />' . $product['list_brief'];
					
					
					$product['price_num'] = $product['price'];
					
					$product['price'] = AZLib::getPrice($product['price'],$product['currency_id']);
						
					$item_news[$product['id']]	  =	$product;
				}
			}
		}
		
		if(User::is_admin())
		{
			$display->add("is_admin",1);
			$display->add("msg",$this->showFormErrorMessages(1));
			$display->add('begin_form',$this->beginForm(true,'post',false,false,1));
			$display->add('end_form',$this->endForm(true));
		}
		
		$display->add('title_new',$title_new);
		$display->add('item_ids',$item_ids);
		$display->add('item_news',$item_news);
		
		$display->output("ItemNew");
	}
}
?>